﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lk.Common.Helper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OAuth.Dapper.Infrastructure.Data;
using OAuth.Dapper.Models;
using OAuth.Server.Domain;
using OAuth.Server.Models.Account;

namespace OAuth.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private IAccountRepository accountRepository;
        private readonly IUnitOfWorkFactory unitOfWorkFactory;

        private readonly ILogger<AccountController> logger;
        public ValuesController(IAccountRepository accountRepository, IUnitOfWorkFactory unitOfWorkFactory, ILogger<AccountController> logger)
        {
            this.accountRepository = accountRepository;
            this.unitOfWorkFactory = unitOfWorkFactory;
            this.logger = logger;
        }


        [Route("account"), HttpGet]
        public async Task<List<AccountInfo>> Get()
        {
      
            logger.LogError("LogError");
            logger.LogInformation("LogInformation");
            logger.LogDebug("LogDebug");

            var result = await accountRepository.GetAccounts();
            //return null;
            return MapperHelper<Account, AccountInfo>.MapList(result);
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public async Task<AccountInfo> Get(int id)
        {
            var result = await accountRepository.GetAccountById(id);
            return MapperHelper<Account, AccountInfo>.Map(result);
        }
        [HttpPost]
        public async Task<IActionResult> Post([FromBody]AccountRequest request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //await _productRepository.AddAsync(prod);

            using (var uow = unitOfWorkFactory.Create())
            {
                await accountRepository.Create(null);

                uow.SaveChanges();
            }

            return NoContent();
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
